﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenerateAdminPage.Classes.FrmClasses
{
    #region USING
    using GenerateAdminPage.Classes.Base;
    using GenerateAdminPage.Classes.Helpers;
    using GenerateAdminPage.Classes.DBStructure;
    #endregion

    public class FrmClassesSelectManyForOne : AbstractFrmClass
    {
        public override string GenerateClass()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB + "public partial class FrmQuanLy" + TblOne.Name + " : Office2007Form");
            Result.AppendLine(TAB + "{");

            Result.AppendLine(GenerateVariables());
            Result.AppendLine(GenerateConstructor());
            Result.AppendLine(GenerateFormLoad());
            Result.AppendLine(GeneratebtnThem_Click());
            Result.AppendLine(GeneratebtnXoa_Click());
            Result.AppendLine(GeneratebtnCapNhat_Click());
            Result.AppendLine(GeneratebtnThoat_Click());
            Result.AppendLine(GenerateInitGridView());
            Result.AppendLine(GenerateLoadData());
            Result.AppendLine(GeneratebtndtgvData_CellClick());

            Result.AppendLine(TAB + "}");

            return Result.ToString();
        }

        public string GeneratebtndtgvData_CellClick()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "if (dtgvData.SelectedRows.Count > 0)");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "FrmLap" + TblOne.Name + " frm = new FrmLap" + TblOne.Name + "(int.Parse(dtgvData.SelectedRows[0].Cells[\"" + Utils.GetPK(TblOne).Name + "\"].Value.ToString()));");
            Result.AppendLine(TAB4 + "frm.Handler += LoadData;");
            Result.AppendLine(TAB4 + "frm.ShowDialog();");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateVariables()
        {
            StringBuilder Result = new StringBuilder();

            if (Utils.HaveFKReferTo(DB, TblOne))
            {
                Result.AppendLine(TAB2 + "public EventHandler Handler { get; set; }");
            }

            return Result.ToString();
        }

        public string GenerateConstructor()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "public FrmQuanLy" + TblOne.Name + "()");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "InitializeComponent();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateFormLoad()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void FrmQuanLy" + TblOne.Name + "_Load(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "InitGridView();");
            Result.AppendLine(TAB3 + "LoadData();");
            Result.AppendLine(TAB3 + "dtgvData.ClearSelection();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnThem_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnThem_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "FrmLap" + TblOne.Name + " frm = new FrmLap" + TblOne.Name + "();");
            Result.AppendLine(TAB3 + "frm.Handler += LoadData;");
            Result.AppendLine(TAB3 + "frm.ShowDialog();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnXoa_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnXoa_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "if (dtgvData.SelectedRows.Count > 0)");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "if (MessageBoxEx.Show(\"Bạn có chắc chắn xóa dòng này không?\", \"Xóa\", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)");
            Result.AppendLine(TAB4 + "{");
            Result.AppendLine(TAB5 + "if (" + TblOne.Name + "Repository.Delete(int.Parse(dtgvData.SelectedRows[0].Cells[\"" + Utils.GetPK(TblOne).Name + "\"].ToString())))");
            Result.AppendLine(TAB5 + "{");
            Result.AppendLine(TAB6 + "MessageBoxEx.Show(\"Xóa dữ liệu thành công\", \"Thông báo\", MessageBoxButtons.OK, MessageBoxIcon.Information);");
            Result.AppendLine(TAB6 + "LoadData();");
            Result.AppendLine(TAB5 + "}");
            Result.AppendLine(TAB5 + "else");
            Result.AppendLine(TAB5 + "{");
            Result.AppendLine(TAB6 + "MessageBoxEx.Show(\"Xóa dữ liệu thất bại\", \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            Result.AppendLine(TAB5 + "}");
            Result.AppendLine(TAB4 + "}");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnCapNhat_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnCapNhat_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "if (dtgvData.SelectedRows.Count > 0)");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "FrmLap" + TblOne.Name + " frm = new FrmLap" + TblOne.Name + "(int.Parse(dtgvData.SelectedRows[0].Cells[\"" + Utils.GetPK(TblOne).Name + "\"].ToString()));");
            Result.AppendLine(TAB4 + "frm.Handler += LoadData;");
            Result.AppendLine(TAB4 + "frm.ShowDialog();");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnThoat_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnThoat_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "this.Close();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateInitGridView()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void InitGridView()");
            Result.AppendLine(TAB2 + "{");

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (i == 0)
                {
                    Result.AppendLine(TAB3 + "DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn");
                }
                else
                {
                    Result.AppendLine(TAB3 + "objColumn = new DataGridViewTextBoxColumn");
                }
                Result.AppendLine(TAB3 + "{");
                Result.AppendLine(TAB4 + "HeaderText = \"" + TblOne.Attributes[i].Name + "\",");
                Result.AppendLine(TAB4 + "DataPropertyName = \"" + TblOne.Attributes[i].Name + "\",");
                Result.AppendLine(TAB4 + "Name = \"" + TblOne.Attributes[i].Name + "\",");

                if (i < TblOne.Attributes.Count - 1)
                {
                    Result.AppendLine(TAB4 + "Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/" + TblOne.Attributes.Count + "))");
                }
                else
                {
                    Result.AppendLine(TAB4 + "Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/" + TblOne.Attributes.Count + ")) - 1");
                }

                Result.AppendLine(TAB3 + "};");
                Result.AppendLine(TAB3 + "dtgvData.Columns.Add(objColumn);");
            }

            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateLoadData()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void LoadData(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "LoadData();");
            Result.AppendLine(TAB2 + "}" + END);

            Result.AppendLine(TAB2 + "private void LoadData()");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "var lstItem = " + TblOne.Name + "Repository.SelectAll();");
            Result.AppendLine(TAB3 + "var dt = new DataTable();");

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                Result.AppendLine(TAB3 + "dt.Columns.Add(\"" + TblOne.Attributes[i].Name + "\");");
            }

            Result.AppendLine(TAB3 + "for (int i = 0; i < lstItem.Count; i++)");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "dt.Rows.Add(" + Utils.BuildListColumnData(TblOne.Attributes) + ");");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "DataView objTimeSheetDV = new System.Data.DataView(dt);");
            Result.AppendLine(TAB3 + "objTimeSheetDV.AllowNew = false;");
            Result.AppendLine(TAB3 + "dtgvData.AutoGenerateColumns = false;");
            Result.AppendLine(TAB3 + "dtgvData.DataSource = objTimeSheetDV;");
            Result.AppendLine(TAB3 + "dtgvData.ClearSelection();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }
    }
}
